package org.harlov.testsys.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.DefaultListModel;

import org.harlov.testsys.loger;
import org.harlov.testsys.db.suppdata.ans_struct;

public class ans_log {
	
	private static db_main DB;
	private static DefaultListModel ans_view;
	private static ArrayList<ans_struct> ans;
	public ans_log (db_main _DB) {
		DB = _DB;
		ans_view = new DefaultListModel();
		ans = new ArrayList<ans_struct>();
	}
	
	public static void getStudAns(int log_id) {
		try {
			ans.clear();
			ResultSet res = DB.makeReq("SELECT * FROM ans_log WHERE test_log_id='"+log_id+"' ORDER BY ans_id ");
			while (res.next()) {
				ans.add(new ans_struct(res.getInt("ans_id"),res.getInt("test_log_id"),res.getInt("quest_id"),res.getArray("f_ans"),res.getArray("t_ans")));
			}
		} catch (SQLException e) {loger.add("getStudAns",null, 1);}
	}
	
	public static DefaultListModel getStudAnsView(int log_id) {
		
		ans_view.clear();
		if (log_id!=-1) {
		
		getStudAns(log_id);
		for (ans_struct temp:ans) {
			ans_view.addElement("Вопрос№"+temp.getQuestId()+" "+temp.isRight());
		}
		}
		return ans_view;
	}
	
	public static int addAns(int test_log_id,int quest_id) {
		int new_id = 0;
		try {
			ResultSet new_ans = DB.makeReq("INSERT INTO ans_log (test_log_id,quest_id) VALUES('"+test_log_id+"','"+quest_id+"') RETURNING ans_id");
			new_ans.next();
			new_id = new_ans.getInt("ans_id");
		} catch (SQLException e) {loger.add(e.getMessage(), null, 1);}
		return new_id;
	}
	
	public static void UpAns(int ans_log_id,Boolean[] fans,Boolean[] tans) {
		try {
			String s_fans = db_main.makeArray(fans);
			String s_tans = db_main.makeArray(tans);			
			DB.makeUp("UPDATE ans_log SET f_ans="+s_fans+",t_ans="+s_tans+" WHERE ans_id="+ans_log_id);
		} catch (SQLException e) {loger.add(e.getMessage(), null, 1);}
	}
	
	
	
	
	

}
